package com.activenetwork.smlite.testcase.events.createEvents.basics;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.model.event.EventBasicType;
import com.activenetwork.smlite.page.event.EventDetailView.EventBasicSubview;
import com.activenetwork.smlite.snippet.EventsSnippet;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.RandomString;
import com.activenetwork.util.verification.gson.FailurePoint;

public class Basics018 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(Basics018.class);
	private EventBasicType.Tag upperCaseTag;
	private EventBasicType.Tag lowerCaseTag;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup(){
		String suffix = RandomString.generateRandomString(5);
		
		upperCaseTag = new EventBasicType.Tag();
		upperCaseTag.setTagName("AUTOTEST-TAG-CASE-"+suffix);
		upperCaseTag.setTagDesc("AUTOTEST-TAG-CASE-DESC");
		upperCaseTag.setPrivate(false);
		
		lowerCaseTag = new EventBasicType.Tag();
		lowerCaseTag.setTagName("autotest-tag-case-"+suffix);
		lowerCaseTag.setTagDesc("autotest-tag-case-desc");
		lowerCaseTag.setPrivate(false);
	}
	
	@Test
	public void Verify_search_tag_is_case_insensitive(){
		loginBusiness.login();
		
		//Create upper case tag
		EventBasicSubview eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		eventBasicSubview = eventBasicSubview.setTag(upperCaseTag);
		eventBasicSubview = eventBasicSubview.saveTag();
		
		// Create lower case tag
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		eventBasicSubview = eventBasicSubview.setTag(lowerCaseTag);
		eventBasicSubview = eventBasicSubview.saveTag();
		
		// Verify the upper case by suggestion list
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		String upperTagNameOnPage = null;
		try{
			eventBasicSubview = eventBasicSubview.selectEventTagAJAX(upperCaseTag);
			By by = By.cssSelector(".autotest-lookup-EventBasicSubview-event-tagsref .nexus-lookup>div[data-lookup-identifier]");
			WebElement tag = webDriver.findElement(by);
			upperTagNameOnPage = tag.getText();
			System.out.println(upperTagNameOnPage);
			
			if(!StringUtils.equals(upperTagNameOnPage, upperCaseTag.getTagName())){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(Basics018.class.getName(), 
						"Verify tag case-sensitive", 
						"Uppercase tag is not found after saving"));
			}
			
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(Basics018.class.getName(), 
					"Verify tag case-sensitive", 
					"Uppercase tag is not found after saving"));
		}
		

		// Verify the lower case by suggestion list
		// lower case is not actually saved to DB
		// so search result should still be upper case tag
		eventBasicSubview = EventsSnippet.gotoAddEventPage();
		
		String lowerTagNameOnPage = null;
		try{
			eventBasicSubview = eventBasicSubview.selectEventTagAJAX(lowerCaseTag);
			By by = By.cssSelector(".autotest-lookup-EventBasicSubview-event-tagsref .nexus-lookup>div[data-lookup-identifier]");
			WebElement tag = webDriver.findElement(by);
			lowerTagNameOnPage = tag.getText();
			System.out.println(lowerTagNameOnPage);
			
			if(!StringUtils.equals(lowerTagNameOnPage, upperCaseTag.getTagName())){
				EnvContext.failurePointTL.get().add(
						new FailurePoint(Basics018.class.getName(), 
						"Verify tag case-sensitive", 
						"Searching lowercase found in suggestion list, but sensitive should be ignored."));
			}
			
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(Basics018.class.getName(), 
					"Verify tag case-sensitive", 
					"Lowercase tag saving error"));
		}
		
		loginBusiness.logout();
		
	}
}
